package cn.zhangtiancinb.simple_demo.http.scheduled;

import java.util.Date;
import java.util.UUID;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;
import org.springframework.scheduling.annotation.Async;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * MQ生产者
 *
 * @author 张天赐
 */
@Slf4j
@Component
public class Producer {

    @Value("${mq.delayed.topic}")
    String topic;
    @Resource
    RabbitTemplate rabbitTemplate;

    /**
     * 一秒一次,生产消息
     */
    @Async
    @Scheduled(fixedRate = 1000)
    public void run() {
        Message message = new Message(UUID.randomUUID().toString().getBytes(), new MessageProperties() {{
            setTimestamp(new Date());
            setMessageId(String.valueOf(UUID.randomUUID()));
        }});
        rabbitTemplate.convertAndSend(topic, message);
        log.info("send once message");
    }
}
